Guards/Tail Recursive in Haskell
Posted on 24 May, 2018 in Haskell
Note: If you want to try coding examples, all you need to install ghci. Then load our functions into the ghci console as modules.
What is functional programming(FP)?
Functional programming is a programming paradigm. The aim is creating code blocks by using pure functions. The pure function has two properties; 1- Given the same inputs to the function always returns the same output 2- The function does not have side effects. By side effects, I mean it does not print something to console etc...
How to define functions in haskell?
Let's look at the following code.
Functions without parameter
-- This is the definition of the function.
returnTenFunction :: Int
-- This part is the function itself.
returnTenFunction = 10
As we can see in the above first we define our function type. Later we define our functions job.
Functions with parameter
-- This is the definition of the function that takes an integer and returns an integer
multiplyByTenFunction :: Int -> Int
-- This part is the function itself.
multiplyByTenFunction value = 10*value
-- We just need to write multiplyByTenFunction 6 in GHCI for calling this function
Infix/prefix in Haskell
Almost everything in Haskell is a function. We can use them as infix or prefix. Let us see the following example.
modPrefixByTen :: Int -> Int
modPrefixByTen value = mod value 10
modInFixByTen :: Int -> Int
modInFixByTen value = value `mod` 10
sumPrefixByTen :: Int -> Int
sumPrefixByTen value = (+) value 10
sumInFixByTen :: Int -> Int
sumInFixByTen value = value + 10
-- We just need to write multiplyByTenFunction 6 in GHCI for calling this function
Guards in Haskell
How do we define if else statement in the function? Let us see.
-- This is the definition of the function that takes an integer and returns an boolean
isItTenFunction :: Int -> Bool
-- This part is the function itself.
isItTenFunction value
| value == 10 = True
| otherwise = False
-- This is the definition of the function that takes two doubles and returns a string
isBigFunction :: Double -> Double -> String
-- This part is the function itself.
isBigFunction valueOne valueTwo
| valueOne < valueTwo = "Small"
| valueOne == valueTwo = "Equal"
| otherwise = "Big"
We have | symbol in the above. We can think it as an if symbol. It checks every condition and acts according to it.
Error in Haskell
divFunction :: Double -> Double -> Double
divFunction valueOne valueTwo
| valueTwo == 0 = error "Division by zero"
| otherwise = (/) valueOne valueTwo
Tail Recursion in Haskell
-- This is the implementation of factorial in Haskell.
factorial :: Int -> Int
factorial n
| n < 0 = error "Negative"
| n == 0 = 1
| otherwise = n * factorial (n-1)
-- This is the implementation of tail recursive factorial in Haskell.
factorial :: Int -> Int
factorial n = factorialHelper 1 n
where
factorialHelper value n
| n < 0 = error "Negative"
| n == 0 = value
| otherwise = factorialHelper (value*n) (n-1)
We basically calculate the value in every function call and return value all in once at tail recursive. where keyword allows us to define local functions. For example, factorialHelper function is seen only by factorial function.